package com.spark.userservice.config;

import com.spark.basecommon.api.R;
import com.spark.basecommon.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fuqiang
 * @Description 全局异常处理类
 * @Date 2020/8/12
 */
@ControllerAdvice
@Slf4j
public class GlobeExceptionHandle {

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public R handBusinessException(HttpServletRequest req, BusinessException e) {
        log.error("运行出错: {}", e.getMessage());
        return R.failed(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R handException(Exception e) {
        log.error("运行出错: {}", e.getMessage());
        return R.failed(e.getMessage());
    }
}
